package net.youyi.crazycopyer.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import net.youyi.crazycopyer.R;
import net.youyi.crazycopyer.common.BlurTransformation;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * Created by xzp on 16/4/11.
 */
public class FastBlurActivity extends BaseActivity{
    ImageView mBlurImage;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fast_blur);
        mBlurImage= (ImageView) this.findViewById(R.id.fast_blur_image);





//        mBlurImage.getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent().requestLayout();




       // mBlurImage.setColorFilter(ContextCompat.getColor(this,R.color.green), PorterDuff.Mode.MULTIPLY);
    }
    public void onSelectPicture(View view){
        startActivityForResult(new Intent(this,SelectPhotoActivity.class),0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode!=RESULT_OK)
            return;
        if(requestCode==0){
            String path=data.getStringExtra("path");
            File file=new File(path);
            if(file.exists()){
                Bitmap bitmap= BitmapFactory.decodeFile(path);
                Bitmap newBitmap=Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas=new Canvas(newBitmap);
                float[] colorTransform = {
                        1f, 0, 0, 0, 0,
                        0, 1f, 0, 0, 1,
                        0, 0, 1f, 0, 0,
                        0, 0, 0, 1, 0};
                ColorMatrix colorMatrix = new ColorMatrix();
                colorMatrix.setSaturation(0f);
                colorMatrix.set(colorTransform);
                ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
                Paint paint = new Paint();
                paint.setColorFilter(colorFilter);
                canvas.drawBitmap(bitmap,0,0,paint);
                mBlurImage.setImageBitmap(newBitmap);
                try {
                    BufferedOutputStream outputStream=new BufferedOutputStream(new FileOutputStream(file));
                    if(file.getName().toLowerCase().endsWith(".jpg") || file.getName().toLowerCase().endsWith(".jpeg"))
                        newBitmap.compress(Bitmap.CompressFormat.JPEG,75,outputStream);
                    else
                        newBitmap.compress(Bitmap.CompressFormat.PNG,75,outputStream);
                } catch (FileNotFoundException e) {

                }
            }
        }
    }
}
